package org.vnl.platform.orm;

import java.util.Collection;

/**
 * 存取Entity物件的控制物件
 * 
 * @author Vaughan
 *
 */
public class AbstractController {

    protected Object find(QueryCommand key) {
        IPersistenceManager pm = PersistenceManagerFactory.getInstance().getCurrentPM();
        return pm.getByKey(key);
    }

    protected Collection<? extends Object> findCustom(QueryCommand query) {
        IPersistenceManager pm = PersistenceManagerFactory.getInstance().getCurrentPM();
        return pm.query(query);
    }
    
    protected void update(Object obj) {
        IPersistenceManager pm = PersistenceManagerFactory.getInstance().getCurrentPM();
        Command cmd = new Command();
        cmd.setEntity(obj);
        pm.update(cmd);
    }
    
    protected void create(Object obj) {
        IPersistenceManager pm = PersistenceManagerFactory.getInstance().getCurrentPM();
        Command cmd = new Command();
        cmd.setEntity(obj);
        pm.create(cmd);
    }
    
    protected void delete(Object obj) {
        IPersistenceManager pm = PersistenceManagerFactory.getInstance().getCurrentPM();
        Command cmd = new Command();
        cmd.setEntity(obj);
        pm.delete(cmd);
    }
    
    protected PageResult findCustomInPage(int currentPage, int pageSize, QueryCommand query) {
        IPersistenceManager pm = PersistenceManagerFactory.getInstance().getCurrentPM();
        return pm.queryPage(currentPage, pageSize, query);
    }

}
